<?php
namespace yanse\controllers;

use common\models\YanseTypeModel;
use common\models\YanseModel;
use common\models\YanseImageModel;
use common\models\YanseImageUrlModel;
use common\models\YanseNovelModel;
use yii\helpers\ArrayHelper;
use Yii;
use yanse\base\BaseController;
use yii\data\Pagination;

/**
 * Site controller
 */
class SiteController extends BaseController
{

    //首页
    public function actionIndex()
    {
        //最近更新
        $select_ids = [50,51,52,70];
        $type_list = YanseTypeModel::find()->select(['id','title'])->where(['in','id',$select_ids])->asArray()->all();
        $select = ['id','title','url','image','time','author','type'];
        $new = YanseModel::find()->select(['id','title','url','image','time','author'])->limit(12)->orderBy('id desc')->asArray()->all();
        $new_arr['id'] = '999';
        $new_arr['title'] = '最近更新';
        $new_arr['list'] = $new;
        $result[0] = $new_arr;
        if(!empty($type_list)){
            foreach($type_list as $k => $val){
                $content = YanseModel::find()->select($select)->where(['type'=>$val['id']])->limit(12)->orderBy('id desc')->asArray()->all();
                if(empty($content)){
                    unset($type_list[$k]);continue;
                }
                $val['list'] = $content;
                $result[] = $val;
            }
        }
        self::setViewParams('','小说,视频,图片','小说,视频,图片');
        return $this->render('index',['type_list'=>$result,'type'=>false]);
    }

    //视频列表
    public function actionList()
    {
        $data = $this->body;
        $id = !empty($data['id']) ? $data['id'] : '';
        $page = !empty($data['page']) ? $data['page'] : 1;
        $type_list = YanseTypeModel::find()->select(['id','title'])->where(['parent'=>$id])->limit(4)->asArray()->all();
        $select = ['id','title','url','image','time','author','type'];
        $count = 0;
        if(!empty($type_list)){
            foreach($type_list as $k => $val){
                $content = YanseModel::find()->select($select)->where(['type'=>$val['id']])->limit(12)->orderBy('id desc')->asArray()->all();
                if(empty($content)){
                    unset($type_list[$k]);continue;
                }
                $type_list[$k]['list'] = $content;
            }
            $type = false;
        }else{
            $type_list = YanseTypeModel::find()->select(['id','title'])->where(['id'=>$id])->asArray()->all();
            $type_list[0]['list'] = YanseModel::find()->select($select)->where(['type'=>$id])->offset(($page-1)*12)->limit(12)->orderBy('id desc')->asArray()->all();
            $count = YanseModel::find()->select($select)->where(['type'=>$id])->orderBy('id desc')->count();
            $type = true;
        }
        self::setViewParams($type_list[0]['title'],$type_list[0]['title'].'小说,视频,图片',$type_list[0]['title'].'小说,视频,图片');
        return $this->render('index',['type_list'=>$type_list,
            'page' => new Pagination(['totalCount' =>$count, 'pageSize' => 12, 'pageSizeParam'=>false]),
            'count'=>$count,'type'=>$type
        ]);
    }

    //视频详情
    public function actionInfo()
    {
        $data = $this->body;
        $id = !empty($data['id']) ? $data['id'] : '';
        $select = ['id','title','url','image','time','author','type','click','desc'];
        $info = YanseModel::find()->select($select)->where(['id'=>$id])->asArray()->one();
        $hot = YanseModel::find()->select($select)->orderBy('click desc')->limit(9)->asArray()->all();
        $like = YanseModel::find()->select($select)->where(['type'=>$info['type']])->limit(10)->asArray()->all();
        self::setViewParams($info['title'],$info['title'],$info['desc']);
        return $this->render('info',['info'=>$info,'hot'=>$hot,'like'=>$like]);
    }

    //帮助页面
    public function actionHelp()
    {
        Yii::$app->layout = false;
        self::setViewParams('','小说,视频,图片','小说,视频,图片');
        return $this->render('help');
    }

    //图片列表
    public function actionImage()
    {
        $data = $this->body;
        $page = !empty($data['page']) ? $data['page'] : 1;
        $list = YanseImageModel::find()->select(['id','title'])->offset(($page-1)*28)->limit(28)->asArray()->all();
        $count = YanseImageModel::find()->count();
        $image = YanseImageUrlModel::find()->select(['image_id','url'])->where(['in','image_id',array_column($list, 'id')])->groupBy('image_id')->asArray()->all();
        $image = ArrayHelper::map($image,'image_id','url');
        if(!empty($list)){
            foreach($list as $k => $val){
                if(isset($image[$val['id']])){
                    $list[$k]['url'] = $image[$val['id']];
                }
            }
        }
        self::setViewParams('',$list[0]['title'].'小说,视频,图片',$list[0]['title'].'小说,视频,图片');
        return $this->render('image',['image'=>$list,'count'=>$count,
            'page' => new Pagination(['totalCount' =>$count, 'pageSize' => 12, 'pageSizeParam'=>false])]);
    }

    //图片详情
    public function actionImageInfo()
    {
        $data = $this->body;
        $id = !empty($data['id']) ? $data['id'] : '';
        $info = YanseImageModel::find()->select(['id','title','time'])->where(['id'=>$id])->asArray()->one();
        $pre = YanseImageModel::find()->select(['id','title'])->where(['id'=>($id-1)])->asArray()->one();
        $next = YanseImageModel::find()->select(['id','title'])->where(['id'=>($id+1)])->asArray()->one();
        $image = YanseImageUrlModel::find()->select(['image_id','url'])->where(['image_id'=>$id])->asArray()->all();
        self::setViewParams($info['title'],$info['title'].'图片',$info['title'].'图片');
        return $this->render('image-info',['info'=>$info,'pre'=>$pre,'next'=>$next,'image'=>$image,]);
    }

    public function actionNovel()
    {
        $data = $this->body;
        $id = !empty($data['id']) ? $data['id'] : '';
        $page = !empty($data['page']) ? $data['page'] : 1;
        $type_list = YanseTypeModel::find()->select(['id','title'])->where(['parent'=>$id,'type'=>2])->limit(4)->asArray()->all();
        $select = ['id','title','author','time','type'];
        $count = 0;
        if(!empty($type_list)){
            foreach($type_list as $k => $val){
                $content = YanseNovelModel::find()->select($select)->where(['type'=>$val['id']])->limit(24)->orderBy('id desc')->asArray()->all();
                if(empty($content)){
                    unset($type_list[$k]);continue;
                }
                $type_list[$k]['list'] = $content;
                $type_list[$k]['count'] = YanseNovelModel::find()->select($select)->where(['type'=>$val['id']])->count();
            }
            $type = false;
        }else{
            $type_list = YanseTypeModel::find()->select(['id','title'])->where(['id'=>$id])->asArray()->all();
            $type_list[0]['list'] = YanseNovelModel::find()->select($select)->where(['type'=>$id])->offset(($page-1)*12)->limit(24)->orderBy('id desc')->asArray()->all();
            $count = YanseNovelModel::find()->select($select)->where(['type'=>$id])->orderBy('id desc')->count();
            $type_list['count'] = $count;
            $type = true;
        }
        self::setViewParams($type_list[0]['title'],$type_list[0]['title'].'小说',$type_list[0]['title'].'小说');
        return $this->render('novel',['type_list'=>$type_list,
            'page' => new Pagination(['totalCount' =>$count, 'pageSize' => 12, 'pageSizeParam'=>false]),
            'count'=>$count,'type'=>$type
        ]);
    }

    //图片详情
    public function actionNovelInfo()
    {
        $data = $this->body;
        $id = !empty($data['id']) ? $data['id'] : '';
        $info = YanseNovelModel::find()->select(['id','title','time','content'])->where(['id'=>$id])->asArray()->one();
        $pre = YanseNovelModel::find()->select(['id','title'])->where(['id'=>($id-1)])->asArray()->one();
        $next = YanseNovelModel::find()->select(['id','title'])->where(['id'=>($id+1)])->asArray()->one();
        self::setViewParams($info['title'],$info['title'].'图片',$info['title'].'图片');
        return $this->render('novel-info',['info'=>$info,'pre'=>$pre,'next'=>$next]);
    }
}

